<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Bootloader Development</title>
		<link rel="stylesheet" type="text/css" href="./images/style.css">
	</head>
	
	<body>
		<header class="banner">
			<h1>PIC18系列MCU 串口自举程序（Bootloader）开发</h1>
		</header>
		
		<main>
			<section>
				<h2>1. 简介</h2>
				<article>
					<p>本示例为基于MCC环境快速开发的PIC18系列MCU(PIC18F16Q41)的串口自举程序。</p>
				</article>
			</section>
			
			<section>
				<h2>2. 工具</h2>
				<article>
					<header>
					<h3>2.1. 软件开发环境</h3>
					</header>
					<p>相关软件开发版本情况说明如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/mplabx" target="_blank">Microchip MPLAB® xIDE version 5.50 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/mplab-xc-compilers" target="_blank">Microchip MPLAB® XC8 C compiler version 2.32 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) version 4.2.4 (with core v5.2.4) or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) Device Libraries PIC10 / PIC12 / PIC16 / PIC18 MCUs version 1.81.7 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip 18-bit Bootloader version 2.4.1 or newer</a></li>
					<li><a href="https://packs.download.microchip.com/" target="_blank">Microchip PIC18F-Q Series Device Support version 1.12.193 or newer</a></li>
					</ul>
				</article>
				<article>
					<header>
					<h3>2.2. 硬件开发环境</h3>
					</header>
					<p>该例程基于如下硬件开发。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/development-tool/EV26Q64A" target="_blank">PIC18F16Q41 CURIOSITY NANO EVALUATION KIT (Part Number: EV26Q64A)</a></li>
					<img src="./images/3359-ev26q64a.jpg" width="20%"; style="margin-left: 40px;">
					</ul>
				</article>
				<article>
					<header>
					<h3>2.3. 参考资料</h3>
					</header>
					<p>相关参考资料如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/product/PIC18F16Q41" target="_blank">PIC18F16Q41 Data Sheet</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tool/EV26Q64A" target="_blank">PIC18F16Q41 Curiosity Nano Hardware User Guide</a></li>
					<li><a href="https://ww1.microchip.com/downloads/en/DeviceDoc/40001779B.pdf" target="_blank">Bootloader Generator User's Guide</a></li>
					</ul>
				</article>
			</section>
			
			<section>
				<h2>3. 自举程序工程创建(仅对关键部分说明)</h2>
				<article>
					<header>
						<h3>3.1. 打开MCC，在Sytem Module下对系统时钟进行如下设置。</h3>
					</header>
					<img src="./images/boot_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.2. 在Sytem Module下,打开Register界面，按下图红框对配置字进行修改，以启动PIC18中断向量表功能。注意：应用程序工程和自举程序工程的配置字需保持一致。</h3>
					</header>
					<img src="./images/boot_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.3. 添加Bootloader Generator库、UART1和MEMORY外设，并对Bootloader Generator进行如下设置。</h3>
					</header>
					<img src="./images/boot_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.4. UART1采用默认配置。</h3>
					</header>
					<img src="./images/boot_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.5. 按下图对相关管脚进行设置，其中RB7是串口发送引脚、RB5是串口接收引脚；RC0是CNANO板的SW0按键。</h3>
					</header>
					<img src="./images/boot_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.6. 生成代码后，对bootloader.h和memory.h做如下更改，其中右侧为修改后文件代码。</h3>
					</header>
					<img src="./images/boot_setup_6.png" width="80%">
					<img src="./images/boot_setup_7.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.7. 设置自举程序工程代码空间范围。</h3>
					</header>
					<img src="./images/boot_setup_8.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.8. 点击下图编译按钮，自举程序工程可成功编译。到此，自举程序工程开发完毕。</h3>
					</header>
					<img src="./images/boot_setup_9.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>4. 应用程序工程创建(仅对关键部分说明)</h2>
				<article>
					<header>
						<h3>4.1. 打开MCC，在Sytem Module下对系统时钟进行如下设置，保证与自举工程程序一致。</h3>
					</header>
					<img src="./images/app_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.2. 在Sytem Module下,打开Register界面，按下图红框对配置字进行修改，以启动PIC18中断向量表功能。注意：应用程序工程和自举程序工程的配置字需保持一致。</h3>
					</header>
					<img src="./images/boot_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.3. 将中断向量表重映射到0xD08，因应用程序工程起始地址为0xD00。</h3>
					</header>
					<img src="./images/app_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.4. 添加外设资源TMR0到项目资源列表中，并按如下设置。该TMR0用于实现CNANO板LED0的500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.5. 按下图对相关管脚进行设置，其中RC1是CNANO板的LED0，RC0是CNANO板的SW0按键。</h3>
					</header>
					<img src="./images/app_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.6. 生成代码后，对main.c文件做如下更改，使能中断，TMR0_UserInterruptHandler()函数用于LED0 500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.7. 设置应用程序工程代码的地址偏移，此偏移根据自举程序工程代码空间范围而定。</h3>
					</header>
					<img src="./images/app_setup_7.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.8. 应用程序工程编译后执行hexmate语句"hexmate ${ImagePath} -FILL=0xFFFF@0x0000:0x0FFFD -CK=0x0D00-0x0FFFD@0x0FFFE+0x0w-2g2 -o${ImagePath}"，该语句用于生成应用程序代码的和校验，校验规则与自举程序工程相匹配。</h3>
					</header>
					<img src="./images/app_setup_8.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.9. 点击下图编译按钮，应用程序工程可成功编译。到此，应用程序工程开发完毕。</h3>
					</header>
					<img src="./images/app_setup_9.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>5. 串口升级实验验证</h2>
				<article>
					<header>
						<h3>5.1. 桌面实验平台搭建如下。</h3>
					</header>
					<img src="./images/demo.jpg" width="40%">
				</article>
				<article>
					<header>
						<h3>5.2. 运行scripts文件夹下的hex_merge_app_boot.bat，将boot和app工程的hex文件合并。</h3>
					</header>
					<img src="./images/verify_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.3. 用USB线缆连接CNANO开发板，运行“MPLAB X IPE”烧录合并后的hex程序，烧录成功后LE0 500ms闪烁。</h3>
					</header>
					<img src="./images/verify_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.4. 断开usb线缆，然后按住SW0按键，再插入USB线缆，紧接着释放SW0按键，进入升级模式。这里需明确CNANO板的串口号，并对scripts文件夹下的serial_upload.bat进行相应修改，之后点击该批处理文件即可完成升级工作。升级成功后，LED0以500ms闪烁。</h3>
					</header>
					<img src="./images/verify_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.5. 若再次升级，请重复5.4步骤。</h3>
					</header>
				</article>
			</section>
		</main>
	</body>
</html>